package controller.base;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import core.modules.base.entity.SysMenu;
import core.modules.base.entity.SysRole;
import core.modules.base.service.SysMenuService;
import core.modules.base.service.SysRoleService;
import core.util.AjaxResult;
import core.util.Page;
import core.util.WebUtil;

@Controller
@RequestMapping("base/sysrole")
public class SysRoleController {
	Logger LOGGER = LoggerFactory.getLogger(SysRoleController.class);

	@Autowired
	SysRoleService SysRoleService;
	@Autowired
	SysMenuService sysMenuService;

	@RequestMapping("list.htm")
	public Object table(SysRole condition, HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		Page<SysRole> page = SysRoleService.findPage(condition);
		map.put("param", condition);
		return "base/sysRole/sysRoleList";
	}

	@RequestMapping("permission.htm")
	public Object permission(String roleid, HttpServletRequest request, HttpServletResponse response,
			ModelMap map) {
		List<SysMenu> menuList = sysMenuService.findList(new SysMenu());
		List<String> permission = sysMenuService.getDao().getMenuIdByRoleId(roleid);
		for (SysMenu sysMenu : menuList) {
			for (String menuid : permission) {
				if (menuid.equals(sysMenu.getId())) {
					sysMenu.setDelflag("1");
				}
			}
		}
		map.put("list", menuList);
		map.put("roleid", roleid);
		return "base/sysRole/permissionForm";
	}
	
	@RequestMapping("savePermission.json")
	@ResponseBody
	public AjaxResult savePermission(String ids, String roleid, HttpServletRequest request,
			HttpServletResponse response) {
		AjaxResult result = null;
		try {
			String[] idsarray = ids.split(",");
			sysMenuService.getDao().deletePermission(roleid);
			for (String menuid : idsarray) {
				if (WebUtil.isNotBlank(menuid)) {
					sysMenuService.getDao().insertPermission(roleid, menuid);
				}
			}
			result = new AjaxResult();
		} catch (Exception e) {
			result = new AjaxResult(false, "保存失败:" + e.getMessage());
		}
		return result;
	}

	@RequestMapping("form.htm")
	public String form(SysRole condition, HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		if (WebUtil.isNotBlank(condition.getId())) {
			condition = SysRoleService.get(condition.getId());
		}
		map.put("param", condition);
		map.put("viewMode", WebUtil.getStr("viewMode", request));
		return "base/sysRole/sysRoleForm";
	}

	@RequestMapping("save.json")
	@ResponseBody
	public AjaxResult save(SysRole condition, HttpServletRequest request, HttpServletResponse response) {
		AjaxResult result = null;
		try {
			SysRoleService.save(condition);
			result = new AjaxResult();
		} catch (Exception e) {
			result = new AjaxResult(false, "保存失败:" + e.getMessage());
		}
		return result;
	}

	@RequestMapping("delete.json")
	@ResponseBody
	public AjaxResult delete(String ids, HttpServletRequest request, HttpServletResponse response) {
		AjaxResult result = null;
		try {
			String[] idArray = ids.split(",");
			for (String id : idArray) {
				if (WebUtil.isNotBlank(id)) {
					SysRoleService.delete(id);
				}
			}
			result = new AjaxResult();
		} catch (Exception e) {
			result = new AjaxResult(false, "删除失败:" + e.getMessage());
		}
		return result;
	}
}
